package javolution.xml;

import java.io.OutputStream;
import java.io.Writer;
import javolution.context.ObjectFactory;
import javolution.lang.Reusable;
import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;
import javolution.xml.stream.XMLStreamWriter;

/* loaded from: classes2.dex */
public class XMLObjectWriter implements Reusable {
    private static final ObjectFactory FACTORY = new ObjectFactory() { // from class: javolution.xml.XMLObjectWriter.1
        /* JADX INFO: Access modifiers changed from: protected */
        @Override // javolution.context.ObjectFactory
        public void cleanup(Object obj) {
            ((XMLObjectWriter) obj).reset();
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // javolution.context.ObjectFactory
        public Object create() {
            return new XMLObjectWriter();
        }
    };
    private boolean _isFactoryProduced;
    private OutputStream _outputStream;
    private Writer _writer;
    private final XMLFormat.OutputElement _xml = new XMLFormat.OutputElement();

    public static XMLObjectWriter newInstance(OutputStream outputStream) throws XMLStreamException {
        XMLObjectWriter xMLObjectWriter = (XMLObjectWriter) FACTORY.object();
        xMLObjectWriter._isFactoryProduced = true;
        xMLObjectWriter.setOutput(outputStream);
        return xMLObjectWriter;
    }

    public static XMLObjectWriter newInstance(OutputStream outputStream, String str) throws XMLStreamException {
        XMLObjectWriter xMLObjectWriter = (XMLObjectWriter) FACTORY.object();
        xMLObjectWriter._isFactoryProduced = true;
        xMLObjectWriter.setOutput(outputStream, str);
        return xMLObjectWriter;
    }

    public static XMLObjectWriter newInstance(Writer writer) throws XMLStreamException {
        XMLObjectWriter xMLObjectWriter = (XMLObjectWriter) FACTORY.object();
        xMLObjectWriter._isFactoryProduced = true;
        xMLObjectWriter.setOutput(writer);
        return xMLObjectWriter;
    }

    /* JADX WARN: Removed duplicated region for block: B:11:0x0037 A[Catch: IOException -> 0x003d, TRY_LEAVE, TryCatch #0 {IOException -> 0x003d, blocks: (B:2:0x0000, B:4:0x0004, B:5:0x0017, B:9:0x0033, B:11:0x0037, B:6:0x001b, B:8:0x001f), top: B:17:0x0000 }] */
    /* JADX WARN: Removed duplicated region for block: B:19:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public void close() throws javolution.xml.stream.XMLStreamException {
        /*
            r2 = this;
            java.io.OutputStream r0 = r2._outputStream     // Catch: java.io.IOException -> L3d
            if (r0 == 0) goto L1b
            javolution.xml.XMLFormat$OutputElement r0 = r2._xml     // Catch: java.io.IOException -> L3d
            javolution.xml.stream.XMLStreamWriterImpl r0 = r0._writer     // Catch: java.io.IOException -> L3d
            r0.writeEndDocument()     // Catch: java.io.IOException -> L3d
            javolution.xml.XMLFormat$OutputElement r0 = r2._xml     // Catch: java.io.IOException -> L3d
            javolution.xml.stream.XMLStreamWriterImpl r0 = r0._writer     // Catch: java.io.IOException -> L3d
            r0.close()     // Catch: java.io.IOException -> L3d
            java.io.OutputStream r0 = r2._outputStream     // Catch: java.io.IOException -> L3d
            r0.close()     // Catch: java.io.IOException -> L3d
        L17:
            r2.reset()     // Catch: java.io.IOException -> L3d
            goto L33
        L1b:
            java.io.Writer r0 = r2._writer     // Catch: java.io.IOException -> L3d
            if (r0 == 0) goto L33
            javolution.xml.XMLFormat$OutputElement r0 = r2._xml     // Catch: java.io.IOException -> L3d
            javolution.xml.stream.XMLStreamWriterImpl r0 = r0._writer     // Catch: java.io.IOException -> L3d
            r0.writeEndDocument()     // Catch: java.io.IOException -> L3d
            javolution.xml.XMLFormat$OutputElement r0 = r2._xml     // Catch: java.io.IOException -> L3d
            javolution.xml.stream.XMLStreamWriterImpl r0 = r0._writer     // Catch: java.io.IOException -> L3d
            r0.close()     // Catch: java.io.IOException -> L3d
            java.io.Writer r0 = r2._writer     // Catch: java.io.IOException -> L3d
            r0.close()     // Catch: java.io.IOException -> L3d
            goto L17
        L33:
            boolean r0 = r2._isFactoryProduced     // Catch: java.io.IOException -> L3d
            if (r0 == 0) goto L3c
            javolution.context.ObjectFactory r0 = javolution.xml.XMLObjectWriter.FACTORY     // Catch: java.io.IOException -> L3d
            r0.recycle(r2)     // Catch: java.io.IOException -> L3d
        L3c:
            return
        L3d:
            r0 = move-exception
            javolution.xml.stream.XMLStreamException r1 = new javolution.xml.stream.XMLStreamException
            r1.<init>(r0)
            goto L45
        L44:
            throw r1
        L45:
            goto L44
        */
        throw new UnsupportedOperationException("Method not decompiled: javolution.xml.XMLObjectWriter.close():void");
    }

    public void flush() throws XMLStreamException {
        this._xml._writer.flush();
    }

    public XMLStreamWriter getStreamWriter() {
        return this._xml._writer;
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._xml.reset();
        this._outputStream = null;
        this._writer = null;
    }

    public XMLObjectWriter setBinding(XMLBinding xMLBinding) {
        this._xml.setBinding(xMLBinding);
        return this;
    }

    public XMLObjectWriter setIndentation(String str) {
        this._xml._writer.setIndentation(str);
        return this;
    }

    public XMLObjectWriter setOutput(OutputStream outputStream) throws XMLStreamException {
        if (this._outputStream == null && this._writer == null) {
            this._xml._writer.setOutput(outputStream);
            this._outputStream = outputStream;
            this._xml._writer.writeStartDocument();
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public XMLObjectWriter setOutput(OutputStream outputStream, String str) throws XMLStreamException {
        if (this._outputStream == null && this._writer == null) {
            this._xml._writer.setOutput(outputStream, str);
            this._outputStream = outputStream;
            this._xml._writer.writeStartDocument();
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public XMLObjectWriter setOutput(Writer writer) throws XMLStreamException {
        if (this._outputStream == null && this._writer == null) {
            this._xml._writer.setOutput(writer);
            this._writer = writer;
            this._xml._writer.writeStartDocument();
            return this;
        }
        throw new IllegalStateException("Writer not closed or reset");
    }

    public XMLObjectWriter setReferenceResolver(XMLReferenceResolver xMLReferenceResolver) {
        this._xml.setReferenceResolver(xMLReferenceResolver);
        return this;
    }

    public void write(Object obj) throws XMLStreamException {
        this._xml.add(obj);
    }

    public void write(Object obj, String str) throws XMLStreamException {
        this._xml.add(obj, str);
    }

    public <T> void write(T t, String str, Class<T> cls) throws XMLStreamException {
        this._xml.add((XMLFormat.OutputElement) t, str, (Class<XMLFormat.OutputElement>) cls);
    }

    public void write(Object obj, String str, String str2) throws XMLStreamException {
        this._xml.add(obj, str, str2);
    }

    public <T> void write(T t, String str, String str2, Class<T> cls) throws XMLStreamException {
        this._xml.add(t, str, str2, cls);
    }
}
